<?php
include_once("sessionmanager.php");
initSession();

if (!isset($_SESSION['s_loggedin']) || ($_SESSION['s_loggedin'] == false) || !isset($_SESSION['s_serverurl']) || (strpos($_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'],$_SESSION['s_serverurl']) === false)) {
	$error_page_code = "timeout";
	include "errorpage.php";
	exit();
}
if (!$_SESSION['s_cansearch']) {
	$error_page_code = "permission";
	include "errorpage.php";
	exit();
}
include "languages.php";
include_once "functions.inc.php";
include "attributes.php";
$userid = $_SESSION['s_userid'];

makeheader(COMBATVIEW_TITLE);
require "config.php";

if (isset($_GET['acoordinates']) && isset($_GET['dcoordinates']) && isset($_GET['combattime'])) {
	$acoordinates = $_GET['acoordinates'];
	$dcoordinates = $_GET['dcoordinates'];
	$combattime = $_GET['combattime'];
	
	mysql_connect($dbhost,$dbusername,$dbpassword) or die(PARSER_ERROR3);
	mysql_select_db($dbname) or die(PARSER_ERROR2);
	if(isset($_GET['delete']) && $_GET['delete'] == 'true' && $_SESSION['s_caninsert']) {
		$query = "DELETE FROM $combatreports where userid=$userid and acoordinates='$acoordinates' and dcoordinates='$dcoordinates' and combattime='$combattime'";
		$res = mysql_query($query);
		if($res) {
			echo "<p align=\"center\">Entry deleted</p>";
		} else {
			echo "<p align=\"center\">" . mysql_error() ."</p>";
		}
	} else {
		$query = "SELECT c.*
		FROM $combatreports c
		WHERE c.userid=$userid and c.acoordinates='$acoordinates' and c.dcoordinates='$dcoordinates' and c.combattime='$combattime'
		ORDER BY c.combattime DESC";
		$res = mysql_query($query);
		echo '<table cellpadding="0" cellspacing="2" border="0" align="center" width="85%">';
	
		if (mysql_num_rows($res) == 0) {
			echo "<tr class=\"tblhead\"><td colspan=\"8\">"."Battle report of ([$acoordinates] attacked [$dcoordinates] at $combattime)"."</td></tr>\n";
			echo "<tr class=\"firstcolor\"><td colspan=\"8\">"."No data"."</td></tr>\n";
		} else {
			$i = 0;
			while ($line = mysql_fetch_object($res)) {
				if($i == 0) {
					echo "<tr class=\"tblhead\"><td colspan=\"3\">"."Battle report of ([$acoordinates] attacked [$dcoordinates] at $combattime)"."</td></tr>\n";
					echo "<tr class=\"tblhead\"><td style=\"width:60px;\"></td><td>"."Attacker"."</td><td>"."Defender"."</td></tr>";
				}
				echo "<tr class=\"firstcolor\" align=\"center\">\n";
				echo "<td class=\"tblhead\" style=\"width:60px;\" align=\"left\">"."Name"."</td>\n";
				echo "<td align=\"center\">".$line->attacker."</td>\n";
				echo "<td align=\"center\">".$line->defender."</td>\n";
				echo "</tr>";
	
				echo "<tr class=\"firstcolor\" align=\"center\">\n";
				echo "<td class=\"tblhead\" style=\"width:60px;\" align=\"left\">"."Coordinates"."</td>\n";
				$tmp = explode(':', $line->acoordinates);
				echo "<td align=\"center\">[<a class=\"farbe1\" href=\"view.php?gala=".$tmp[0]."&system=".$tmp[1]."\">".$line->acoordinates."</a>]</td>\n";
				$tmp = explode(':', $line->dcoordinates);
				echo "<td align=\"center\">[<a class=\"farbe1\" href=\"view.php?gala=".$tmp[0]."&system=".$tmp[1]."\">".$line->dcoordinates."</a>]</td>\n";
				echo "</tr>";
				
				echo "<tr class=\"firstcolor\" align=\"center\">\n";
				echo "<td class=\"tblhead\" style=\"width:60px;\" align=\"left\">"."Winner"."</td>\n";
				echo "<td align=\"center\">". ($line->winner=='attacker'? '*' : ($line->winner=='deuce'? '-' : ''))."</td>\n";
				echo "<td align=\"center\">". ($line->winner=='defender'? '*' : ($line->winner=='deuce'? '-' : ''))."</td>\n";
				echo "</tr>";			
				
				echo "<tr class=\"firstcolor\" align=\"center\">\n";
				echo "<td class=\"tblhead\" style=\"width:60px;\" align=\"left\">"."Lost"."</td>\n";
				echo "<td align=\"center\">". $line->alost_total ."</td>\n";
				echo "<td align=\"center\">". $line->dlost_total ."</td>\n";
				echo "</tr>";			
	
				echo "<tr class=\"firstcolor\" align=\"center\">\n";
				echo "<td class=\"tblhead\" style=\"width:60px;\" align=\"left\">"."Plunder"."</td>\n";
				echo "<td align=\"center\">". ($line->p_metal>0 || $line->p_crystal>0 || $line->p_deuterium>0 ? "M:". $line->p_metal. "<br/>C:". $line->p_crystal ."<br/>D:". $line->p_deuterium : "-") ."</td>\n";
				echo "<td align=\"center\">". "" ."</td>\n";
				echo "</tr>";
	
				echo "<tr class=\"firstcolor\" align=\"center\">\n";
				echo "<td class=\"tblhead\" style=\"width:60px;\" align=\"left\">"."Debris"."</td>\n";
				echo "<td align=\"center\" colspan=\"2\">". ($line->debris_metal>0 || $line->debris_crystal>0 ? "M:". $line->debris_metal. "    C:". $line->debris_crystal : "-") ."</td>\n";
				echo "</tr>";
	
				echo "<tr class=\"firstcolor\" align=\"center\">\n";
				echo "<td class=\"tblhead\" style=\"width:60px;\" align=\"left\">"."Text"."</td>\n";
				echo "<td align=\"left\" colspan=\"2\">". str_replace("\n", "<br/>\n", $line->report) ."</td>\n";
				echo "</tr>";
							
				$i++;
			}
			echo "<tr class=\"tblhead\"><td colspan=\"3\">("."END".")</td></tr>\n";
			if($_SESSION['s_caninsert']) {
				echo "<tr class=\"tblhead\"><td colspan=\"3\" align=\"right\">"."<a href=\"combatview.php?acoordinates=$acoordinates&dcoordinates=$dcoordinates&combattime=$combattime&delete=true\">delete</a>"."</td></tr>\n";
			}
	   		echo "<tr class=\"tblhead\"><td colspan=\"3\" align=\"left\">"."<a href=\"combatview.php\">List all</a>"."</td></tr>\n";
		}
	echo "</table>";
  }
} else if (!isset($_GET['acoordinates']) && !isset($_GET['dcoordinates']) && !isset($_GET['combattime'])) {
	
	mysql_connect($dbhost,$dbusername,$dbpassword) or die(PARSER_ERROR3);
	mysql_select_db($dbname) or die(PARSER_ERROR2);
	$query = "SELECT c.* FROM $combatreports c WHERE c.userid=$userid ";
	if(isset($_GET['attacker']) && !empty($_GET['attacker'])) $query .= " AND attacker='" . $_GET['attacker'] ."'";
	if(isset($_GET['defender']) && !empty($_GET['defender'])) $query .= " AND defender='" . $_GET['defender'] ."'";
	if(isset($_GET['coordinates']) && !empty($_GET['coordinates'])) {
		if(isset($_GET['flag']) && !empty($_GET['flag'])){
			if($_GET['flag'] == 's') $query .= " AND acoordinates='" . $_GET['coordinates'] ."'";
			else if($_GET['flag'] == 'd') $query .= " AND dcoordinates='" . $_GET['coordinates'] ."'";
		}
	}
	$query .= " ORDER BY c.combattime DESC";
	if(isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page']>0) $page = $_GET['page'];
	else $page=1;
	$pagecount = 20;
	$query .= " LIMIT ". (($page-1)*$pagecount) . ", " . ($pagecount+1);
	$res = mysql_query($query);
	if(!$res) {
		echo mysql_error();
		return;
	}
	echo '<table cellpadding="0" cellspacing="2" border="0" align="center" width="85%">';
	$rows = mysql_num_rows($res);
	if ( $rows == 0) {
		echo "<tr class=\"tblhead\"><td colspan=\"8\">"."Battle report"."</td></tr>\n";
		echo "<tr class=\"firstcolor\"><td colspan=\"8\">"."No data"."</td></tr>\n";
	} else {
		$i = 0;
		while (($line = mysql_fetch_object($res)) && ($i<$pagecount)) {
			if($i == 0) {
				echo "<tr class=\"tblhead\"><td colspan=\"9\">"."Battle report (List All)"."</td></tr>\n";
				echo "<tr class=\"tblhead\"><td style=\"width:30px;\">Ind.</td><td>"."Attacker"."</td><td>"."Coord"."</td><td>"."Defender"."</td><td>"."Coord"."</td><td>"."Time"."</td><td>"."Winner"."</td><td>"."Plunder"."</td><td>"."Debris"."</td></tr>";
			}
			echo "<tr class=\"firstcolor\" align=\"center\">\n";
			echo "<td style=\"width:30px;\" align=\"center\">"."<a class=\"link\" href=\"combatview.php?acoordinates=$line->acoordinates&dcoordinates=$line->dcoordinates&combattime=$line->combattime\">".($i+1)."</a>"."</td>\n";
			echo "<td align=\"center\">"."<a class=\"link\" href=\"combatview.php?attacker=$line->attacker\">".$line->attacker."</a></td>\n";
			echo "<td align=\"center\">["."<a class=\"link\" href=\"combatview.php?flag=s&coordinates=$line->acoordinates\">".$line->acoordinates."</a>]</td>\n";
			echo "<td align=\"center\">"."<a class=\"link\" href=\"combatview.php?defender=$line->defender\">".$line->defender."</td>\n";
			echo "<td align=\"center\">["."<a class=\"link\" href=\"combatview.php?flag=d&coordinates=$line->dcoordinates\">".$line->dcoordinates."</a>]</td>\n";
			echo "<td align=\"center\">".$line->combattime."</td>\n";
			echo "<td align=\"center\">".($line->winner=='attacker'? 'A' : ($line->winner=='deuce'? '-' : 'D'))."</td>\n";
			echo "<td align=\"center\">". ($line->p_metal>0 || $line->p_crystal>0 || $line->p_deuterium>0 ? "M:". $line->p_metal. "<br/>C:". $line->p_crystal ."<br/>D:". $line->p_deuterium : "-") ."</td>\n";
			echo "<td align=\"center\" colspan=\"2\">". ($line->debris_metal>0 || $line->debris_crystal>0 ? "M:". $line->debris_metal. "<br/>C:". $line->debris_crystal : "-") ."</td>\n";
			echo "</tr>";
			
			$i++;
		}
		$qs = preg_replace('/&?page=[^%&\s]*/', '',$_SERVER['QUERY_STRING']);
		if(!empty($qs)) $qs = $qs . "&";
		echo "<tr class=\"tblhead\"><td colspan=\"9\">" . 
		($page>1 ? "<a class=\"link\" href=\"combatview.php?$qs" . "page=". ($page-1) ."\">prev&lt;</a>" : "") . 
		"&nbsp;$page&nbsp;" .
		($rows>$pagecount ? "<a class=\"link\" href=\"combatview.php?$qs". "page=". ($page+1) ."\">&gt;next</a>" : "") .
		"</td></tr>\n";
		echo "<tr class=\"tblhead\"><td colspan=\"9\">("."END".")</td></tr>\n";
   	
	}
	echo "<tr class=\"tblhead\">" . "<form method=\"GET\" action=\"combatview.php\">". "<td colspan=\"9\" align=\"left\">". 
	"search by destination coordinates: " . 
	"<input class=\"textfield\" name=\"coordinates\" value=\"\" style=\"width:80px;height:15px;\" maxlength=\"15\">". 
	"<input type=\"hidden\" name=\"flag\" value=\"d\"/>" .
	"</td>" . "</form>". "</tr>\n";	
	echo "</table>";
}
?>

<?php
makefooter();
?>